package za.co.dylanjaneke.airdownloader.util
{
	import flash.utils.describeType;
	
	import za.co.dylanjaneke.airdownloader.controller.data.ConnectionManager;
	import za.co.dylanjaneke.airdownloader.model.vo.LogEntryVO;

	public class ViLogger
	{
		private var loc:String;
		
		public function ViLogger(clazz:Class)
		{
			this.loc= describeType(clazz).@name;
		}
		
		public function info(message:String): void
		{
			traceAndSave("INFO", message);
		}
		
		public function debug(message:String): void
		{
			traceAndSave("DEBUG", message);
		}
		
		public function warn(message:String): void
		{
			traceAndSave("WARN", message);
		}
		
		public function panic(message:String): void
		{
			traceAndSave("PANIC", message);
		}
		
		public function error(message:String): void
		{
			traceAndSave("ERROR", message);
		}
		
		private function traceAndSave(lev:String, message:String): void
		{
			var vo:LogEntryVO= new LogEntryVO(
				new Date(), lev, message, this.loc);
			trace(vo);
			ConnectionManager.getInstance().executeStatement(
				LogEntryVO.createInsert(vo, 
					ConnectionManager.getInstance().createStatement()));
		}
	}
}